package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.cardview.widget.CardView;
import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemTeamIntelligenceBinding;
import com.sgb.kjwl.databinding.PopwindowTeamIntelligenceBinding;
import com.sgb.kjwl.model.entity.enterprise.ServiceQuoteEntity;
import com.sgb.kjwl.model.entity.enterprise.TeamIntelligenceEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DisplayUtil;
import com.sgb.kjwl.view.ui.activity.enterprise.ProfessionalTeamPictureActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class TeamIntelligenceAdapter extends BaseRecycleAdapter<TeamIntelligenceEntity> {

    Context context;
    List<String> list = new ArrayList<>();
    public TeamIntelligenceAdapter(Context context, List<TeamIntelligenceEntity> datas) {
        super(context, R.layout.item_team_intelligence, datas);
        this.context = context;
    }

    @Override
    public void convert(ItemViewHolder holder, TeamIntelligenceEntity teamIntelligenceEntity) {
        holder.setBinding(BR.viewModel, teamIntelligenceEntity);

        ItemTeamIntelligenceBinding itemTeamIntelligenceBinding = (ItemTeamIntelligenceBinding) holder.getmBinding();
        itemTeamIntelligenceBinding.tvDate.setText(TextViewBindingAdapter.setTextForColon("资质类型:"+teamIntelligenceEntity.getTypeName()));
        itemTeamIntelligenceBinding.tvPrice.setText(TextViewBindingAdapter.setTextForColon("资质等级:"+teamIntelligenceEntity.getGradeName().getName()));
        holder.getItem().findViewById(R.id.tv_show_detail).setOnClickListener(
                view -> showDialog(teamIntelligenceEntity,holder.getPosition()));

        holder.getItem().findViewById(R.id.iv_item_team_intelligent_image).setOnClickListener(view ->{


            showImage(teamIntelligenceEntity,holder.getPosition());
        });
    }



    private void showImage(TeamIntelligenceEntity teamIntelligenceEntity,int position){
        list.clear();

        list.add(datas.get(position-1).getUploadQualification());


        ActivityJump.showBigPicture(context,list,0,true);
    }

    private void showDialog(TeamIntelligenceEntity teamIntelligenceEntity,int position) {
        DisplayMetrics dm = new DisplayMetrics();
        //获取屏幕信息
        dm = context.getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels;
        int screenHeigh = dm.heightPixels;

        LayoutInflater inflater = LayoutInflater.from(context);
        PopwindowTeamIntelligenceBinding layout = DataBindingUtil.inflate(inflater,R.layout.popwindow_team_intelligence, null,false);
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(layout.getRoot());
        builder.setCancelable(true);
        dialog = builder.show();
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        CardView cv = dialog.findViewById(R.id.cv_dialog);
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) cv.getLayoutParams(); //取控件textView当前的布局参数
        linearParams.width = DisplayUtil.dip2px(context,270);//
        linearParams.height = screenHeigh / 3;//
        cv.setLayoutParams(linearParams);

        if (teamIntelligenceEntity != null) {
            layout.setViewModel(teamIntelligenceEntity);
            layout.tvTime.setText(TextViewBindingAdapter.setTextForColon("资质类型:"+teamIntelligenceEntity.getTypeName()));
            layout.tvDate.setText(TextViewBindingAdapter.setTextForColon("资质等级:"+teamIntelligenceEntity.getGradeName().getName()));
            layout.imageHeand.setOnClickListener(view ->{
//                showImage(teamIntelligenceEntity,position);
            });
        }

        dialog.findViewById(R.id.iv_cancel).setOnClickListener(v -> dialog.dismiss());
    }
}